<template>
  <div class="login-container">
    <!-- 左侧品牌区 -->
    <div class="brand-panel">
      <img src="@/assets/logo.png" alt="logo" class="logo" />
      <h1 class="brand-title">潮汐智能云图库</h1>
      <p class="brand-desc">企业级智能协同 · 安全 · 高效</p>
    </div>

    <!-- 右侧登录区 -->
    <div class="form-panel">
      <div class="form-card">
        <h2 class="form-title">欢迎回来</h2>
        <div class="form-desc">登录后继续管理您的图片资产</div>

        <a-form
          :model="formState"
          name="login"
          autocomplete="off"
          @finish="handleSubmit"
          @finishFailed="handleError"
        >
          <a-form-item
            name="userAccount"
            :rules="[{ required: true, message: '请输入账号' }]"
          >
            <a-input
              v-model:value="formState.userAccount"
              size="large"
              placeholder="账号 / 手机号"
            >
              <template #prefix>
                <UserOutlined />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item
            name="userPassword"
            :rules="[
              { required: true, message: '请输入密码' },
              { min: 8, message: '密码不少于 8 位' },
            ]"
          >
            <a-input-password
              v-model:value="formState.userPassword"
              size="large"
              placeholder="密码"
            >
              <template #prefix>
                <LockOutlined />
              </template>
            </a-input-password>
          </a-form-item>

          <a-form-item>
            <a-checkbox v-model:checked="remember">记住账号</a-checkbox>
            <RouterLink to="/user/reset" class="forget-link">
              忘记密码？
            </RouterLink>
          </a-form-item>

          <a-form-item>
            <a-button
              type="primary"
              html-type="submit"
              size="large"
              :loading="loading"
              block
            >
              登录
            </a-button>
          </a-form-item>

          <div class="tips">
            还没有账号？
            <RouterLink to="/user/register">立即注册</RouterLink>
          </div>
        </a-form>
      </div>

      <!-- 页脚 -->
      <GlobalFooter class="inner-footer" theme="light" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { message } from 'ant-design-vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import GlobalFooter from '@/components/GlobalFooter.vue'
import { userLoginUsingPost } from '@/api/userController'

const router = useRouter()
const route = useRoute()
const loginUserStore = useLoginUserStore()

interface FormState {
  userAccount: string
  userPassword: string
}

const formState = reactive<FormState>({
  userAccount: '',
  userPassword: '',
})
const remember = ref(false)
const loading = ref(false)

// 记住账号初始化
if (localStorage.rememberAccount === 'true') {
  formState.userAccount = localStorage.account || ''
  remember.value = true
}

const handleSubmit = async () => {
  loading.value = true
  try {
    const res = await userLoginUsingPost(formState)
    if (res.data.code === 0) {
      // 更新全局状态
      loginUserStore.setLoginUser(res.data.data)
      // 记住账号
      if (remember.value) {
        localStorage.rememberAccount = 'true'
        localStorage.account = formState.userAccount
      } else {
        localStorage.removeItem('rememberAccount')
        localStorage.removeItem('account')
      }
      message.success('登录成功')
      // 重定向到之前页或首页
      const redirect = (route.query.redirect as string) || '/'
      
      // 验证 redirect 是否为站内路径，防止重定向到外部网站
      let redirectPath = '/'
      if (redirect && typeof redirect === 'string' && redirect.startsWith('/')) {
        // 解码 redirect 参数
        const decodedRedirect = decodeURIComponent(redirect);
        // 确保不是跳转到登录或注册页面
        if (!decodedRedirect.startsWith('/user/login') && !decodedRedirect.startsWith('/user/register')) {
          redirectPath = decodedRedirect
        }
      }
      
      router.replace(redirectPath)
    } else {
      message.error(res.data.message || '登录失败')
    }
  } finally {
    loading.value = false
  }
}

const handleError = () => message.warning('请检查输入')
</script>

<style scoped>
.login-container {
  display: flex;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

/* 品牌面板 */
.brand-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px;
}
.logo {
  width: 80px;
  margin-bottom: 16px;
}
.brand-title {
  font-size: 32px;
  margin: 0 0 8px;
}
.brand-desc {
  font-size: 16px;
  opacity: 0.9;
}

/* 表单面板 */
.form-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
}
.form-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px;
}
.form-title {
  margin: 0 0 8px;
  font-size: 24px;
}
.form-desc {
  color: #999;
  margin-bottom: 32px;
}
.forget-link {
  float: right;
  font-size: 13px;
}
.tips {
  text-align: center;
  font-size: 13px;
  color: #666;
}
.inner-footer {
  margin-top: 40px;
  background: transparent !important;
  box-shadow: none !important;
}

/* 响应式 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }
  .brand-panel {
    display: none;
  }
  .form-panel {
    padding: 24px;
  }
}
</style>
